<style scoped>
</style>
<template>
    <div id='myApp'>
        <Row style="margin-bottom: 40px;border-bottom: 1px solid #eeeeee;">
            <Col span="5" style="border-bottom:2px solid #2d8cf0 ;padding: 0 0 8px 18px;">
            <span style="color: #2d8cf0;">
                <Icon type="md-add-circle" />&nbsp;&nbsp;
                <span style="color: black;">系统管理&nbsp;&nbsp;>&nbsp;&nbsp;</span>设定
            </span>
            </Col>
        </Row>
        <Row>
            <Col span="12" offset="4">
            <Form ref="ruleForm" :rules="ruleValidate" :model="ruleForm" :label-width="180">
                <FormItem prop="drillAddress" label="apache drill地址：">
                    <Input v-model="ruleForm.drillAddress" />
                </FormItem>
                <FormItem prop="mongdbAddress" label="mongdb地址：">
                    <Input v-model="ruleForm.mongdbAddress" />
                </FormItem>
                <FormItem prop="accessRestriction" label="接口访问限制：">
                    <Row style="margin-bottom: 10px;">
                        <Col span="6">
                        <span style="float: right;">
                            1分钟最大
                        </span>
                        </Col>
                        <Col span="17" offset="1">
                            <span style="float: left;">
                                <Input type="number" v-model="ruleForm.accessRestriction.oneMin"/>
                            </span>
                            &nbsp;次
                        </Col>
                    </Row>
                    <Row style="margin-bottom: 10px;">
                        <Col span="6">
                        <span style="float: right;">
                            10分钟最大
                        </span>
                        </Col>
                        <Col span="17" offset="1">
                            <span style="float: left;">
                                <Input type="number" v-model="ruleForm.accessRestriction.tenMin"/>
                            </span>
                            &nbsp;次
                        </Col>
                    </Row>
                    <Row style="margin-bottom: 10px;">
                        <Col span="6">
                        <span style="float: right;">
                            半小时最大
                        </span>
                        </Col>
                        <Col span="17" offset="1">
                            <span style="float: left;">
                                <Input type="number" v-model="ruleForm.accessRestriction.halfHour"/>
                            </span>
                            &nbsp;次
                        </Col>
                    </Row>
                    <Row>
                        <Col span="6">
                        <span style="float: right;">
                            1小时最大
                        </span>
                        </Col>
                        <Col span="17" offset="1">
                            <span style="float: left;">
                                <Input type="number" v-model="ruleForm.accessRestriction.oneHour"/>
                            </span>
                            &nbsp;次
                        </Col>
                    </Row>
                </FormItem>
                <FormItem prop="cacheExpirationTime" label="缓存过期时间：">
                    <Input v-model="ruleForm.cacheExpirationTime" />
                </FormItem>
                <FormItem>
                    <Button type="primary" style="width: 100px;">保存</Button>
                </FormItem>
            </Form>
            </Col>
        </Row>


    </div>
</template>
<script>
    import { mapState, mapActions } from 'vuex'
    export default {
        computed: mapState({
            Authorization: state => state.Authorization
        }),
        data() {
            return {
                ruleForm: {
                    drillAddress:'',
                    mongdbAddress:'',
                    cacheExpirationTime:'',
                    accessRestriction:{
                        oneMin:'',
                        tenMin:'',
                        halfHour:'',
                        oneHour:''
                    }
                },
                ruleValidate: {
                }
            }
        },
        mounted() {
            //isEdit = 0 为添加网站；isEdit = 1 为编辑网站 
            this.isEdit = this.$route.params.type;
            if (this.$route.params.type == 1) {
                this.ruleForm.id = this.$route.params.id;
                console.log(this.ruleForm.id)
                this.getDetail(this.ruleForm.id);
            }
        },
        methods: {
            getDetail(id) {
                //根据id查询站点详细内容，然后填充到页面上
                this.$axios({
                    methods: 'GET',
                    url: '/api/site?' + Math.random(),
                    params: {
                        token: this.Authorization,
                        id: id,
                    },
                    headers: {
                        'Content-type': 'application/x-www-form-urlencoded'
                    }
                })
                    .then(response => {
                        let res = response.data
                        if (res.code == 0) {
                            let i = 0
                            this.ruleForm.id = res.data.id
                            this.ruleForm.name = res.data.name
                            this.ruleForm.homepage = res.data.homepage
                            this.ruleForm.domain = res.data.domain
                            let entries = ''
                            let accepted = ''
                            let refused = '' 
                            let articleAccepted = ''
                            let articleRefused = ''
                            for (i = 0; i < res.data.entries.length; i++) {
                                entries = entries + res.data.entries[i] + '\n'
                            }
                            for (i = 0; i < res.data.accepted.length; i++) {
                                accepted = accepted + res.data.accepted[i] + '\n'
                            }
                            for (i = 0; i < res.data.refused.length; i++) {
                                refused = refused + res.data.refused[i] + '\n'
                            }
                            for (i = 0; i < res.data.articleAccepted.length; i++) {
                                articleAccepted = articleAccepted + res.data.articleAccepted[i] + '\n'
                            }
                            for (i = 0; i < res.data.articleRefused.length; i++) {
                                articleRefused = articleRefused + res.data.articleRefused[i] + '\n'
                            }
                            this.ruleForm.entries = entries
                            this.ruleForm.accepted = accepted
                            this.ruleForm.refused = refused
                            this.ruleForm.articleAccepted = articleAccepted
                            this.ruleForm.articleRefused = articleRefused
                            this.ruleForm.renderMode = res.data.renderMode
                            this.ruleForm.updateIntervalMinutes = res.data.updateIntervalMinutes
                            this.ruleForm.maxDepth = res.data.maxDepth
                        } else if (res.code != 0) {
                            // alert(res.message);
                        }
                    })
                    .catch(function (error) {
                        console.log(error);
                        alert('初始化失败！');
                    });
            },
            submit0: function () {
                //isEdit = 0 为添加网站；isEdit = 1 为编辑网站  update为post  create为put
                let url = '/api/site?' + Math.random()
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        console.log('this.ruleForm.renderMode = ' + this.ruleForm.renderMode)
                        let _this = this;
                        let renderMode = this.ruleForm.renderMode == true ? 1 : 0
                        let data = this.$qs.stringify({
                            token: this.Authorization,
                            id: this.ruleForm.id,
                            name: this.ruleForm.name,
                            homepage: this.ruleForm.homepage,
                            domain: this.ruleForm.domain,
                            entries: this.ruleForm.entries,
                            accepted: this.ruleForm.accepted,
                            refused: this.ruleForm.refused,
                            articleAccepted: this.ruleForm.articleAccepted,
                            articleRefused: this.ruleForm.articleRefused,
                            updateIntervalMinutes: this.ruleForm.updateIntervalMinutes,
                            maxDepth: this.ruleForm.maxDepth,
                            // renderMode: this.ruleForm.renderMode//renderMode为0/1，ruleForm.renderMode为true/false
                            // true为1 false为0
                            renderMode: renderMode
                        })
                        this.$axios
                            .put(url, data)
                            .then(response => {
                                let res = response.data
                                console.log(response);
                                if (res.code == 0) {
                                    this.$Message.success('保存成功!');
                                    _this.$router.replace({ path: '/websiteManage' });
                                } else if (res.code != 0) {
                                    this.$Message.error(res.message);
                                }
                            })
                            .catch(function (error) {
                                console.log(error);
                            });
                    }
                })

            },
            submit1: function () {
                //isEdit = 0 为添加网站；isEdit = 1 为编辑网站  update为post  create为put
                let url = '/api/site?' + Math.random()
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        let _this = this;
                        let renderMode = this.ruleForm.renderMode == true ? 1 : 0
                        let data = this.$qs.stringify({
                            token: this.Authorization,
                            id: this.ruleForm.id,
                            name: this.ruleForm.name,
                            homepage: this.ruleForm.homepage,
                            domain: this.ruleForm.domain,
                            entries: this.ruleForm.entries,
                            accepted: this.ruleForm.accepted,
                            refused: this.ruleForm.refused,
                            articleAccepted: this.ruleForm.articleAccepted,
                            articleRefused: this.ruleForm.articleRefused,
                            updateIntervalMinutes: this.ruleForm.updateIntervalMinutes,
                            maxDepth: this.ruleForm.maxDepth,
                            // renderMode: this.ruleForm.renderMode//renderMode为0/1，ruleForm.renderMode为true/false
                            // true为1 false为0
                            renderMode: renderMode
                        })
                        this.$axios
                            .post(url, data)
                            .then(response => {
                                let res = response.data
                                console.log(response);
                                if (res.code == 0) {
                                    this.$Message.success('保存成功!');
                                    // setTimeout("_this.$router.replace({ path: '/websiteManage' });",1000);
                                    _this.$router.replace({ path: '/websiteManage' });
                                } else if (res.code != 0) {
                                    this.$Message.error(res.message);
                                }
                            })
                            .catch(function (error) {
                                console.log(error);
                            });
                    }
                })

            },
            full0: function () {//向后台发送请求,立即全站扫描
                //isEdit = 0 为添加网站；isEdit = 1 为编辑网站  update为post  create为put
                let url = '/api/site?' + Math.random()
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        let _this = this;
                        let renderMode = this.ruleForm.renderMode == true ? 1 : 0
                        let data1 = this.$qs.stringify({
                            token: this.Authorization,
                            id: this.ruleForm.id,
                            name: this.ruleForm.name,
                            homepage: this.ruleForm.homepage,
                            domain: this.ruleForm.domain,
                            entries: this.ruleForm.entries,
                            accepted: this.ruleForm.accepted,
                            refused: this.ruleForm.refused,
                            articleAccepted: this.ruleForm.articleAccepted,
                            articleRefused: this.ruleForm.articleRefused,
                            updateIntervalMinutes: this.ruleForm.updateIntervalMinutes,
                            maxDepth: this.ruleForm.maxDepth,
                            // renderMode: this.ruleForm.renderMode//renderMode为0/1，ruleForm.renderMode为true/false
                            // true为1 false为0
                            renderMode: renderMode
                        })
                        this.$axios
                            .put(url, data1)
                            .then(response => {
                                let res = response.data
                                console.log(response);
                                if (res.code == 0) {
                                    this.$Message.success('保存成功!');
                                    let data2 = this.$qs.stringify({
                                        token: this.Authorization,
                                        id: this.ruleForm.id
                                    })
                                    this.$axios
                                        .post('api/site/full_fetch/new?' + Math.random(), data2)
                                        .then(response1 => {
                                            let res1 = response1.data
                                            if (res1.code == 0) {
                                                this.$Message.success('全站扫描已开启!');
                                            }
                                        })
                                        .catch(function (error) {
                                            console.log(error)
                                        });



                                    _this.$router.replace({ path: '/websiteManage' });
                                } else if (res.code != 0) {
                                    this.$Message.error(res.message);
                                }
                            })
                            .catch(function (error) {
                                console.log(error);
                            });


                    }
                })
            },
            full1:function(){
                //isEdit = 0 为添加网站；isEdit = 1 为编辑网站  update为post  create为put
                let url = '/api/site?' + Math.random()
                this.$refs['ruleForm'].validate((valid) => {
                    if (valid) {
                        let _this = this;
                        let renderMode = this.ruleForm.renderMode == true ? 1 : 0
                        let data = this.$qs.stringify({
                            token: this.Authorization,
                            id: this.ruleForm.id,
                            name: this.ruleForm.name,
                            homepage: this.ruleForm.homepage,
                            domain: this.ruleForm.domain,
                            entries: this.ruleForm.entries,
                            accepted: this.ruleForm.accepted,
                            refused: this.ruleForm.refused,
                            articleAccepted: this.ruleForm.articleAccepted,
                            articleRefused: this.ruleForm.articleRefused,
                            updateIntervalMinutes: this.ruleForm.updateIntervalMinutes,
                            maxDepth: this.ruleForm.maxDepth,
                            // renderMode: this.ruleForm.renderMode//renderMode为0/1，ruleForm.renderMode为true/false
                            // true为1 false为0
                            renderMode: renderMode
                        })
                        this.$axios
                            .post(url, data)
                            .then(response => {
                                let res = response.data
                                console.log(response);
                                if (res.code == 0) {
                                    this.$Message.success('保存成功!');
                                    let data2 = this.$qs.stringify({
                                        token: this.Authorization,
                                        id: this.ruleForm.id
                                    })
                                    this.$axios
                                        .post('api/site/full_fetch/new?' + Math.random(), data2)
                                        .then(response1 => {
                                            let res1 = response1.data
                                            if (res1.code == 0) {
                                                this.$Message.success('全站扫描已开启!');
                                            }
                                        })
                                        .catch(function (error) {
                                            console.log(error)
                                        });


                                    _this.$router.replace({ path: '/websiteManage' });
                                } else if (res.code != 0) {
                                    this.$Message.error(res.message);
                                }
                            })
                            .catch(function (error) {
                                console.log(error);
                            });
                    }
                })
            }
        }
    }
</script>